package com.lagou.service;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPObject;
import com.lagou.Vo.LinkVo;
import com.lagou.Vo.Result;
import com.lagou.bean.Person;
import com.lagou.repository.PersonRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service("personService")
public class Neo4jPersonService {
    @Autowired
    private PersonRepository personRepository;
    public List<Person>  getAll(){
        List<Person>  datas = new ArrayList<>();
        personRepository.findAll().forEach(person -> datas.add(person));
        return  datas;
    }
    public  Person  save(Person person){
        return  personRepository.save(person);
    }
    public  List<Person>  personList(double money){
        return  personRepository.personList(money);
    }

    public Result getAllPersonByStartNameAndName(String startName, String endName,Boolean isShortPath){
        List<Person> personList = new ArrayList<>();
        if(isShortPath){
            personList = personRepository.getAllPersonByStartNameAndNameShortestPath(startName, endName);
        }else{
            personList = personRepository.getAllPersonByStartNameAndName(startName, endName);
        }
        List<LinkVo> linkList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(personList)){
            personList.forEach(person -> {
                handleRelationShip(person,person.getFriendsPerson(),linkList,"Friends","朋友");
                handleRelationShip(person,person.getCouplePerson(),linkList,"Couple","夫妻");
                handleRelationShip(person,person.getFatherPerson(),linkList,"Father","父子");
                handleRelationShip(person,person.getWifeMotherPerson(),linkList,"Wife_Mother","岳母");
                handleRelationShip(person,person.getSonPerson(),linkList,"Son","儿子");
                handleRelationShip(person,person.getBrotherSisterPerson(),linkList,"BrotherSister","兄弟姐妹");
                handleRelationShip(person,person.getMotherPerson(),linkList,"Mother","母亲");
            });
        }
        JSONObject result = new JSONObject();
        result.put("data",personList);
        result.put("links",linkList);


        return Result.ok(result);
    }
    public  List<Person> personListDept(String name){
        return  personRepository.personListDept(name);
    }


    /**
     * 处理关系
     * @param person 源对象，关系都是 ->
     * @param relationPerson 所有出去的关系
     * @param linkList 供前台展示的关系数据
     */
    private void handleRelationShip(Person person,Set<Person> relationPerson ,List<LinkVo> linkList,String type,String desc){
        if(!CollectionUtils.isEmpty(relationPerson)){
            relationPerson.forEach(relationP ->{
                LinkVo linkVo = new LinkVo(person.getId().toString(),relationP.getId().toString(),type,desc);
                linkList.add(linkVo);
            });
        }

    }



}
